﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

using Aden.Model.Common;
using Aden.Model.Stock;

namespace Aden.DAL.StockData
{
    public class DailyTicket : Stockout
    {
        private StockDataFactory sofactory = new StockDataFactory();
        public DailyTicket(string appGuid,string costCenterCode, string orderDate, string createUser)
            :base(appGuid, costCenterCode,"DailyTicket", orderDate, createUser) {}

        public override List<Dictionary<string, object>> GetItems(StockParam param) // Dictionary<string, string> param
        {
            List<Dictionary<string, object>> SOItems
                  = sofactory.GetDailyTicketList(DateTime.Parse(OrderDate), CostCenterCode).Select(dr =>
                  {
                      Dictionary<string, object> dic = new Dictionary<string, object>();
                      dic.Add("orderLineGUID", dr.orderLineGUID);
                      dic.Add("itemCode", dr.itemCode);
                      dic.Add("itemName", dr.itemName);
                      dic.Add("uom", dr.uom);
                      dic.Add("itemPrice", dr.itemPrice);
                      dic.Add("isServiceItem", dr.isServiceItem);
                      return dic;
                  }).ToList();
            return SOItems;
        }

        public override List<Dictionary<string,object>> GetDates()
        {
            List<Dictionary<string, object>> SODates
                = sofactory.GetDailyTicketDates(CostCenterCode).Select(dr =>
                {
                    Dictionary<string, object> dic = new Dictionary<string, object>();
                    dic.Add("date", ((DateTime)dr.date).ToString("yyyy-MM-dd"));
                    dic.Add("Remark", dr.remark);
                    return dic;
                }).ToList();
            return SODates;
        }

        public override Dictionary<string, object> save(StockParam param) // Dictionary<string, string> param, List<TransData> data
        {
            try
            {
                var tmp = param.data.Where(q => q.qty>0);
                if (!tmp.Any()) throw new Exception("No Daily Ticket to receive");
                
                base.save(param.data, null);
                return null;
            }
            catch (Exception e)
            {
                throw e;
            }

        }

        public override Dictionary<string, object> ShowComponents()
        {
            Dictionary<string, object> dic = new Dictionary<string, object>();
           
            dic["Item"] = new Dictionary<string, object>() { { "change", false } };
            dic["ItemDesc"] = new Dictionary<string, object>() { { "visible", true } };
            dic["Price"] = new Dictionary<string, object>() { { "visible", true }, { "change", false } };
            dic["QrderQty"] = new Dictionary<string, object>() { { "visible", false } };
            dic["StockQty"] = new Dictionary<string, object>() { { "visible", false } };
            dic["ToCostCenter"] = new Dictionary<string, object>() { { "visible", false } };
            dic["BusinessType"] = new Dictionary<string, object>() { { "visible", false }, { "change", false } };
            dic["InQty"] = new Dictionary<string, object>() { { "visible", false } };
            dic["Remark"] = new Dictionary<string, object>() { { "visible", true }, { "change", true } };
            return dic;
        }
    }
}